﻿#include "deviceconversion.h"
#include <QDebug>
#include "../commons/configfile.h"
#include "../commons/regionalpins.h"
#include "../commons/pinsinfors.h"
#include "../commons/cleaninfors.h"

#include "../logs/printlogs.h"

DeviceConversion * DeviceConversion::m_deviceConversion = NULL;
DeviceConversion::DeviceConversion(QObject *parent) : QObject(parent)
  , m_serial(NULL)
  , m_outTypes(0) // 1同时出料  2一起出料 3混合出料
  , m_outTimer(NULL)
  , m_outCmdTimer(NULL)
  , m_clearDataError(0)
  , m_cleanTimer(NULL)
{
    m_comAlgor = new ComAlgor();
    m_serial = new QSerialPort();
    connect(m_serial,SIGNAL( readyRead() ),this,SLOT( onDataReceived() ));
    m_outTimer = new QTimer(this);
    connect(m_outTimer ,SIGNAL( timeout() ) ,this ,SLOT( onOutTimeout() ) );
    m_outCmdTimer = new QTimer(this);
    connect(m_outCmdTimer ,SIGNAL( timeout() ) ,this ,SLOT( onOutCmdTimeout() ) );
    m_cleanTimer = new QTimer(this);
    connect(m_cleanTimer ,SIGNAL( timeout() ) ,this ,SLOT( onCleanTimeout() ) );


    initComSeriel();
}


DeviceConversion::~DeviceConversion()
{

}

DeviceConversion *DeviceConversion::gestance()
{
    if(m_deviceConversion == NULL){
        m_deviceConversion = new DeviceConversion();
    }

    return m_deviceConversion;
}

void DeviceConversion::initComSeriel()
{
    QString names =  ConfigFile::gestance()->getCom2Name();
#ifdef DEBUG_INFOR
    qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"names=="<<names;
#endif
    if(names.length() > 0){
#ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__;
#endif
        QSerialPortInfo info(names);
        m_serial->setPort(info);
        m_serial->setBaudRate(QSerialPort::Baud115200);
        m_serial->setParity(QSerialPort::NoParity);
        m_serial->setDataBits(QSerialPort::Data8);
        m_serial->setStopBits(QSerialPort::OneStop);
        m_serial->setFlowControl(QSerialPort::NoFlowControl);

        if(m_serial->open(QIODevice::ReadWrite)) // 以读写方式打开串口
        {
#ifdef DEBUG_INFOR
            qDebug()<< metaObject()->className()<<__func__<<__LINE__;
#endif

        }
#ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__;
#endif

    }
}

void DeviceConversion::closeComSeriel()
{

    if(m_serial != NULL){
        m_serial->close();
    }
#ifdef DEBUG_INFOR
    qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"names=="<<m_serial->error();

#endif
}

void DeviceConversion::openComSeriel()
{
    if(m_serial != NULL){
        if(m_serial->open(QIODevice::ReadWrite)) // 以读写方式打开串口
        {
#ifdef DEBUG_INFOR
            qDebug()<< metaObject()->className()<<__func__<<__LINE__;
#endif

        }
    }
}

void DeviceConversion::handlLed(QMap<int, QPair<int, int> > leds)
{
#ifdef DEBUG_INFOR
    qDebug()<< metaObject()->className()<<__func__<<__LINE__<<leds;
#endif
#ifndef DEBUG_INFOR
    QString printlogStr=typeid(this).name()+QString(" %1").arg( __func__) +" -- "+ QString("%1").arg( __LINE__)   ;
    PrintLogb::gestance()->printInfor(printlogStr);
#endif
    //    if(true){
    //        return;
    //    }
    char ch[256];
    uchar lens =0;
    uchar lenb =0;
    uchar cha = 0;
    int i = 0;
    ch[0]='W';
    ch[1]='X';
    ch[2]=0x01;
    ch[3]=0x01;
    ch[4]=0x00;
    ch[5]=0x01;
    ch[6]=0x21;

    int lina =0;
    QList<int> lista =  leds.keys();
    for(int m = 0 ; m <lista.size() ; m++,lina++){
        QPair<int, int> paira= leds.value(lista.at(m));
        ch[8 +m* 2]=32+paira.first;
        ch[9 +m* 2]=paira.second;

    }
    lina = 6;
    cha = ch[0];
    ch[7]=lina;
    for(int k =1 ; k <= 7 + lina;k++){
        cha += ch[k];
    }
    ch[8 + lina] = cha;

    QByteArray by((char * )ch,9+ lina);
#ifdef DEBUG_INFOR
    qDebug()<< metaObject()->className()<<__func__<<__LINE__<<by.toHex();
#endif
    if(m_serial != NULL){
        m_serial->write(by);
        m_serial->flush();
    }
}

void DeviceConversion::handlSequentialDischarge(uint8_t cmds, QList<int> lits,QMap<int, QPair< int ,int > > infors , QList<int> sorts)
{
#ifndef DEBUG_INFOR
    QString printlogStr=typeid(this).name()+QString(" %1").arg( __func__) +" -- "+ QString("%1").arg( __LINE__) + QString("%1").arg(cmds)  ;
    PrintLogb::gestance()->printInfor(printlogStr);
#endif
    m_outData.clear();
    m_outTypes = 1;
    m_currentCmd = cmds;
    m_outInfors.clear();
    m_outInfors = infors;
    m_outList = lits;
    m_outSorts= sorts;
#ifdef DEBUG_INFOR
    qDebug()<< metaObject()->className()<<__func__<<__LINE__<<m_outInfors;
#endif
    handlOutCmd();
    return;
    QList<QPair<int ,int> > infra;
    QList<QPair<int ,int> > infrb;
    QList<QPair<int ,int> > infrc;


    QSet<int> litsa = RegionalPins::gestance()->getPins("A");
    QSet<int> litsb = RegionalPins::gestance()->getPins("B");
    QSet<int> litsc = RegionalPins::gestance()->getPins("C");
    //  QList<int> testa;
    //  QList<int> testb;
    // QList<int> testc;
    // QList<int> listsa = infors.keys();
    for(int i = 0; i < lits.size() ;i++){
        int pins = lits.at(i);
        if(litsa.contains(pins)){
            infra.append(infors.value(pins));
            continue;
        }
        if(litsb.contains(pins)){
            infrb.append(infors.value(pins));
            continue;
        }
        if(litsc.contains(pins)){
            infrc.append(infors.value(pins));
            continue;
        }
    }

    if(infra.size() > 0){
        m_outData.insert("A",infra);
    }
    if(infrb.size() > 0){
        m_outData.insert("B",infrb);
    }
    if(infrc.size() > 0){
        m_outData.insert("C",infrc);
    }
    if(m_outData.size() > 0){
        QString regins =  m_outData.keys().at(0);
        m_regions = regins;
        handlReginLed(m_regions,1);
        //    m_outTimer->start(RegionalPins::gestance()->getRegionalTimers(m_regions));
    }
}

void DeviceConversion::handlReginLed(QString regins,int stats)
{
#ifndef DEBUG_INFOR
    QString printlogStr=typeid(this).name()+QString(" %1").arg( __func__) +" -- "+ QString("%1").arg( __LINE__) + QString("%1").arg(regins)  ;
    PrintLogb::gestance()->printInfor(printlogStr);
#endif
    int lens = 0;
    int lena = 0;

    m_regnLed.insert(regins,stats);
    uint8_t tempb = 0;
    uint8_t buffer[250];

    buffer[0] = 'W';
    buffer[1] = 'X';

    buffer[2] = 0x01;
    buffer[3] = 0x01;
    buffer[4] = 0x00;

    buffer[5] = 0x01;
    buffer[6] = 0x23;
#ifdef DEBUG_INFOR
    qDebug()<< metaObject()->className()<<__func__<<__LINE__;
#endif
    if (regins.contains("A")) {
        buffer[8] = (uint8_t) 1;//(byte) ((tracks >> 8) & 0xFF);
    }
    if (regins.contains("B")) {
        buffer[8] = (uint8_t) 2;//(byte) ((tracks >> 8) & 0xFF);
    }
    if (regins.contains("C")) {
        buffer[8] = (uint8_t) 3;//(byte) ((tracks >> 8) & 0xFF);
    }

    buffer[9] = (uint8_t) stats;//(byte) (tracks & 0xFF);


#ifdef DEBUG_INFOR
    qDebug()<< metaObject()->className()<<__func__<<__LINE__;
#endif
    lens = 2;
    buffer[7] = (uint8_t) lens;
    for (int i = 0; i <= 9; i++) {

        tempb += buffer[i];
    }
    buffer[10] = tempb;

    lena = 11;
    QByteArray tempa((char * )buffer,  lena);
#ifdef DEBUG_INFOR
    qDebug()<< metaObject()->className()<<__func__<<__LINE__<<tempa.toHex();
#endif
    if(m_serial != NULL){
#ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__;
#endif
        m_serial->write(tempa);
#ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__;
#endif
        m_serial->flush();
#ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__;
#endif
    }
}

void DeviceConversion::handlCmdLed(QMap<int, int> leds)
{
#ifndef DEBUG_INFOR
    QString printlogStr=typeid(this).name()+QString(" %1").arg( __func__) +" -- "+ QString("%1").arg( __LINE__)   ;
    PrintLogb::gestance()->printInfor(printlogStr);
#endif
    int lens = 0;
    int lena = 0;


    uint8_t tempb = 0;
    uint8_t buffer[250];

    buffer[0] = 'W';
    buffer[1] = 'X';

    buffer[2] = 0x01;
    buffer[3] = 0x01;
    buffer[4] = 0x00;

    buffer[5] = 0x01;
    buffer[6] = 0x23;
#ifdef DEBUG_INFOR
    qDebug()<< metaObject()->className()<<__func__<<__LINE__;
#endif
    QMap<int, int>::iterator ita = leds.begin();
    for(int i = 0;ita !=leds.end() ;ita++ ,i+=2){
        buffer[8+i] = (uint8_t)ita.key();
        buffer[9+i] = (uint8_t) ita.value();
        m_regnInforsLed.insert(ita.key(), ita.value());
    }


#ifdef DEBUG_INFOR
    qDebug()<< metaObject()->className()<<__func__<<__LINE__;
#endif
    lens = leds.size()* 2;
    buffer[7] = (uint8_t) lens;
    for (int i = 0; i <= 7+lens; i++) {

        tempb += buffer[i];
    }
    buffer[7+lens + 1] = tempb;

    lena = 7+lens+2;
    QByteArray tempa((char * )buffer,  lena);
#ifdef DEBUG_INFOR
    qDebug()<< metaObject()->className()<<__func__<<__LINE__<<tempa.toHex();
#endif
    if(m_serial != NULL){
#ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__;
#endif
        m_serial->write(tempa);
#ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__;
#endif
        m_serial->flush();
#ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__;
#endif
    }
}

void DeviceConversion::handlOutCmd()
{
#ifndef DEBUG_INFOR
    QString printlogStr=typeid(this).name()+QString(" %1").arg( __func__) +" -- "+ QString("%1").arg( __LINE__)   ;
    PrintLogb::gestance()->printInfor(printlogStr);
#endif
#ifdef DEBUG_INFOR
    qDebug()<< metaObject()->className()<<__func__<<__LINE__<<m_outData;
#endif
    //QList<QPair<int ,int> > lista =  m_outData.value(m_regions);
    // qDebug()<< metaObject()->className()<<__func__<<__LINE__<<lista;
    int lens = 0;
    int lena = 0;
#ifdef DEBUG_INFOR
    qDebug()<< metaObject()->className()<<__func__<<__LINE__<<m_outInfors;
#endif
    uint8_t tempb = 0;
    uint8_t buffer[300];

    buffer[0] = 'W';
    buffer[1] = 'X';

    buffer[2] = 0x01;
    buffer[3] = 0x01;
    buffer[4] = 0x00;

    buffer[5] = 0x01;
    buffer[6] = 0x62;
    int index = 0;
    int nmbera = 0;
    int indexa = 0;
    int times = 0;
#ifdef DEBUG_INFOR
    qDebug()<< metaObject()->className()<<__func__<<__LINE__;
#endif
    if(m_outTypes == 1){
#ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__;
#endif
        // QMap<int, QPair< int ,int > >::iterator ita =   m_outInfors.begin();
        //for (; ita != m_outInfors.end() ; ita++) {  //  for (int k = 0; k < lista.size(); k++) {
        //    QPair<int ,int> paira =  ita.value();
        for (int k = 0; k < m_outSorts.size(); k++) {
            int sorta = m_outSorts.at(k);
            QPair<int ,int> paira =  m_outInfors.value(sorta);
#ifdef DEBUG_INFOR
            qDebug()<< metaObject()->className()<<__func__<<__LINE__;
#endif
            for (indexa = 0; indexa < 1; indexa++) {
#ifdef DEBUG_INFOR
                qDebug()<< metaObject()->className()<<__func__<<__LINE__;
#endif
                int baseid = paira.first;
                int outdata = paira.second;
                PinData * itema = PinsInfors::gestance()->getPin(sorta);
                if(itema == NULL){
                    continue;
                }
                int drits = itema->m_direction.toInt();
                int speeda = itema->m_speed.toInt();
                int compenTime =(int) (itema->m_compenTime.toFloat() * 1000);

                int timeas = (int) outdata + compenTime;
                times +=timeas;
#ifdef DEBUG_INFOR
                qDebug()<< metaObject()->className()<<__func__<<__LINE__<<baseid;
                qDebug()<< metaObject()->className()<<__func__<<__LINE__<<compenTime;
                qDebug()<< metaObject()->className()<<__func__<<__LINE__<<timeas;
                qDebug()<< metaObject()->className()<<__func__<<__LINE__<<outdata;
#endif
                buffer[9 + nmbera + 7 * indexa] = (uint8_t) baseid;//(byte) (tracks & 0xFF);
                buffer[10 + nmbera + 7 * indexa] = (uint8_t) drits;
                buffer[11 + nmbera + 7 * indexa] = (uint8_t) speeda;
                buffer[12 + nmbera + 7 * indexa] = (uint8_t) speeda;

                buffer[13 + nmbera + 7 * indexa] = (uint8_t) ((timeas >> 16) & 0xFF);

                buffer[14 + nmbera + 7 * indexa] = (uint8_t) ((timeas >> 8) & 0xFF);

                buffer[15 + nmbera + 7 * indexa] = (uint8_t) ((timeas) & 0xFF);
                index += 7;
            }
#ifdef DEBUG_INFOR
            qDebug()<< metaObject()->className()<<__func__<<__LINE__;
#endif
            buffer[8 + nmbera] = (uint8_t) (7 * indexa);//(uint8_t) ((tracks >> 8) & 0xFF);
            nmbera += 7 * indexa + 1;
            index += 1;
        }
#ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__;
#endif
        emit sigHandlRunOutTime( times * 2 + 2000);
    }
    if(m_outTypes == 2){
#ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__;
#endif
        for (int k = 0; k < 1; k++) {
#ifdef DEBUG_INFOR
            qDebug()<< metaObject()->className()<<__func__<<__LINE__;
#endif
            QMap<int, QPair< int ,int > >::iterator ita =   m_outInfors.begin();
            for (indexa = 0; ita != m_outInfors.end() ; ita++,indexa++) {  //  for (indexa = 0; indexa < lista.size(); indexa++) {
#ifdef DEBUG_INFOR
                qDebug()<< metaObject()->className()<<__func__<<__LINE__;
#endif
                QPair<int ,int> paira =   ita.value();//lista.at(indexa);
                int baseid = paira.first;
                int outdata = paira.second;
                PinData * itema = PinsInfors::gestance()->getPin(ita.key());
                if(itema == NULL){
                    continue;
                }
                int drits = itema->m_direction.toInt();
                int speeda = itema->m_speed.toInt();
                int compenTime =(int) (itema->m_compenTime.toFloat() * 1000);

                int timeas = (int) outdata + compenTime;
                if(times <= timeas ){
                    times =timeas;
                }
#ifdef DEBUG_INFOR
                qDebug()<< metaObject()->className()<<__func__<<__LINE__<<baseid;
                qDebug()<< metaObject()->className()<<__func__<<__LINE__<<compenTime;
                qDebug()<< metaObject()->className()<<__func__<<__LINE__<<timeas;
                qDebug()<< metaObject()->className()<<__func__<<__LINE__<<outdata;
#endif
                buffer[9 + nmbera + 7 * indexa] = (uint8_t) baseid;//(byte) (tracks & 0xFF);
                buffer[10 + nmbera + 7 * indexa] = (uint8_t) drits;
                buffer[11 + nmbera + 7 * indexa] = (uint8_t) speeda;
                buffer[12 + nmbera + 7 * indexa] = (uint8_t) speeda;

                buffer[13 + nmbera + 7 * indexa] = (uint8_t) ((timeas >> 16) & 0xFF);

                buffer[14 + nmbera + 7 * indexa] = (uint8_t) ((timeas >> 8) & 0xFF);

                buffer[15 + nmbera + 7 * indexa] = (uint8_t) ((timeas) & 0xFF);
                index += 7;
            }
#ifdef DEBUG_INFOR
            qDebug()<< metaObject()->className()<<__func__<<__LINE__;
#endif
            buffer[8 + nmbera] = (uint8_t) (7 * indexa);//(uint8_t) ((tracks >> 8) & 0xFF);
            nmbera += 7 * indexa + 1;
            index += 1;
        }
#ifdef DEBUG_INFOR

        qDebug()<< metaObject()->className()<<__func__<<__LINE__;
#endif
        emit sigHandlRunOutTime( times* 2 + 2000);
    }

    lens = index;
    buffer[7] = (uint8_t) lens;
    for (int i = 0; i <= 7 + index; i++) {
#ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__;
#endif
        tempb += buffer[i];
    }
    buffer[8 + index] = tempb;

    lena = 9 + index;
    QByteArray tempa((char *)buffer,  lena);
#ifdef DEBUG_INFOR
    qDebug()<< metaObject()->className()<<__func__<<__LINE__<<tempa.toHex();
#endif
    if(m_serial != NULL){
#ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__;
#endif
        m_serial->write(tempa);
#ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__;
#endif
        m_serial->flush();
#ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__;
#endif
    }
#ifdef DEBUG_INFOR
    qDebug()<< metaObject()->className()<<__func__<<__LINE__;
#endif
    // m_outData.remove(m_regions);
}

void DeviceConversion::handlSameDischarge(uint8_t cmds, QList<int> lits, QMap<int, QPair<int, int> > infors , QList<int> sorts)
{
#ifndef DEBUG_INFOR
    QString printlogStr=typeid(this).name()+QString(" %1").arg( __func__) +" -- "+ QString("%1").arg( __LINE__) + QString("%1").arg(cmds)  ;
    PrintLogb::gestance()->printInfor(printlogStr);
#endif
    m_outData.clear();
    m_outTypes = 2;
    m_currentCmd = cmds;
    m_outInfors.clear();
    m_outInfors = infors;
    m_outSorts = sorts;
    handlOutCmd();
    return;
    QList<QPair<int ,int> > infra;
    QList<QPair<int ,int> > infrb;
    QList<QPair<int ,int> > infrc;
#ifdef DEBUG_INFOR
    qDebug()<< metaObject()->className()<<__func__<<__LINE__<<infors;
    qDebug()<< metaObject()->className()<<__func__<<__LINE__<<lits;
#endif

    QSet<int> litsa = RegionalPins::gestance()->getPins("A");
    QSet<int> litsb = RegionalPins::gestance()->getPins("B");
    QSet<int> litsc = RegionalPins::gestance()->getPins("C");
#ifdef DEBUG_INFOR
    qDebug()<< metaObject()->className()<<__func__<<__LINE__<<litsa;
    qDebug()<< metaObject()->className()<<__func__<<__LINE__<<litsb;
    qDebug()<< metaObject()->className()<<__func__<<__LINE__<<litsc;
#endif
    //  QList<int> testa;
    //  QList<int> testb;
    // QList<int> testc;
    // QList<int> listsa = infors.keys();
    for(int i = 0; i < lits.size() ;i++){
        int pins = lits.at(i);
        if(litsa.contains(pins)){
            infra.append(infors.value(pins));
            continue;
        }
        if(litsb.contains(pins)){
            infrb.append(infors.value(pins));
            continue;
        }
        if(litsc.contains(pins)){
            infrc.append(infors.value(pins));
            continue;
        }
    }
#ifdef DEBUG_INFOR
    qDebug()<< metaObject()->className()<<__func__<<__LINE__;
    qDebug()<< metaObject()->className()<<__func__<<__LINE__<<infra;
    qDebug()<< metaObject()->className()<<__func__<<__LINE__<<infrb;
    qDebug()<< metaObject()->className()<<__func__<<__LINE__<<infrc;
#endif
    if(infra.size() > 0){
        m_outData.insert("A",infra);
    }
    if(infrb.size() > 0){
        m_outData.insert("B",infrb);
    }
    if(infrc.size() > 0){
        m_outData.insert("C",infrc);
    }
#ifdef DEBUG_INFOR
    qDebug()<< metaObject()->className()<<__func__<<__LINE__<<m_outData;
#endif
    if(m_outData.size() > 0){
        QString regins =  m_outData.keys().at(0);
        m_regions = regins;
#ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__<<m_regions;
#endif
        handlReginLed(m_regions,1);
        //    m_outTimer->start(RegionalPins::gestance()->getRegionalTimers(m_regions));
    }
}

void DeviceConversion::setCleanIndex(int indexs)
{
#ifndef DEBUG_INFOR
    QString printlogStr=typeid(this).name()+QString(" %1").arg( __func__) +" -- "+ QString("%1").arg( __LINE__) + QString("%1").arg(indexs)  ;
    PrintLogb::gestance()->printInfor(printlogStr);
#endif
    m_cleanIndexs = indexs;
    m_selectCleanIndexs = 0;
    m_clearDataError = 0;
#ifdef DEBUG_INFOR
    qDebug()<< metaObject()->className()<<__func__<<__LINE__<<m_cleanIndexs;
#endif
    m_cleanList = CleanInfors::gestance()->getPin(m_cleanIndexs);
#ifdef DEBUG_INFOR
    qDebug()<< metaObject()->className()<<__func__<<__LINE__<<m_cleanList;
#endif
    handlCleanCmd();
}

int DeviceConversion::getCleanError()
{
    return  m_clearDataError;
}

void DeviceConversion::handlCleanCmd()
{
#ifndef DEBUG_INFOR
    QString printlogStr=typeid(this).name()+QString(" %1").arg( __func__) +" -- "+ QString("%1").arg( __LINE__) + QString("%1").arg(m_selectCleanIndexs)  ;
    PrintLogb::gestance()->printInfor(printlogStr);
#endif
#ifdef DEBUG_INFOR
    qDebug()<< metaObject()->className()<<__func__<<__LINE__<<m_selectCleanIndexs;
#endif
    int timeas = 0;
    if(m_selectCleanIndexs < m_cleanList.size() ){
        m_currentTimes = QDateTime::currentDateTime().toTime_t();
        QJsonObject jsona = m_cleanList.at(m_selectCleanIndexs);
#ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__<<jsona;
#endif
        int ishot = jsona.take("ishot").toInt();
        if(ishot == 100){
            char ch[256];
            uchar lens =0;
            uchar lenb =0;
            uchar cha = 0;
            int i = 0;
            ch[0]='W';
            ch[1]='X';
            ch[2]=0x01;
            ch[3]=0x01;
            ch[4]=0x00;
            ch[5]=0x01;
            ch[6]=0x70;

            int lina =0;
            int m = 0;
            QJsonArray group = jsona.take("group").toArray();
            for( m = 0 ; m < group.size(); m++){
                QJsonObject itema = group.at(m).toObject();
                int foreTime = itema.take("foreTime").toInt();
                int reverTime = itema.take("reverTime").toInt();
                int speed = itema.take("speed").toInt();
                timeas+= foreTime;
                timeas+= reverTime;

                ch[8 +m* 8]=(uchar)(foreTime >> 16);
                ch[9 +m* 8]=(uchar)(foreTime >> 8);
                ch[10 +m* 8]=(uchar)(foreTime );

                ch[11 +m* 8]=(uchar)(reverTime >> 16);
                ch[12 +m* 8]=(uchar)(reverTime >> 8);
                ch[13 +m* 8]=(uchar)(reverTime );

                ch[14 +m* 8]=(uchar)(speed );
                ch[15 +m* 8]=(uchar)(speed );
            }
            lina = m  *8;
            cha = ch[0];
            ch[7]=lina;
            for(int k =1 ; k <= 7 + lina;k++){
                cha += ch[k];
            }
            ch[8 + lina] = cha;

            QByteArray by((char * )ch,9+ lina);
#ifdef DEBUG_INFOR
            qDebug()<< metaObject()->className()<<__func__<<__LINE__<<by.toHex();
#endif
            if(m_serial != NULL){
                m_serial->write(by);
                m_serial->flush();
            }
            timeas+= 800000;

            timeas+= 50000;

            emit   sigCleanRunTimeout(timeas);
        }


        if(ishot == 101){
            char ch[256];
            uchar lens =0;
            uchar lenb =0;
            uchar cha = 0;
            int i = 0;
            ch[0]='W';
            ch[1]='X';
            ch[2]=0x01;
            ch[3]=0x01;
            ch[4]=0x00;
            ch[5]=0x01;
            ch[6]=0x68;

            int lina =0;
            int m = 0;
            QJsonArray group = jsona.take("group").toArray();
            for( m = 0 ; m < group.size(); m++){
                QJsonObject itema = group.at(m).toObject();
                int foreTime = itema.take("foreTime").toInt();
                int reverTime = itema.take("reverTime").toInt();
                int speed = itema.take("speed").toInt();
                timeas+= foreTime;
                timeas+= reverTime;

                ch[8 +m* 8]=(uchar)(foreTime >> 16);
                ch[9 +m* 8]=(uchar)(foreTime >> 8);
                ch[10 +m* 8]=(uchar)(foreTime );

                ch[11 +m* 8]=(uchar)(reverTime >> 16);
                ch[12 +m* 8]=(uchar)(reverTime >> 8);
                ch[13 +m* 8]=(uchar)(reverTime );

                ch[14 +m* 8]=(uchar)(speed );
                ch[15 +m* 8]=(uchar)(speed );
            }
            lina = m  *8;
            cha = ch[0];
            ch[7]=lina;
            for(int k =1 ; k <= 7 + lina;k++){
                cha += ch[k];
            }
            ch[8 + lina] = cha;

            QByteArray by((char * )ch,9+ lina);
#ifdef DEBUG_INFOR
            qDebug()<< metaObject()->className()<<__func__<<__LINE__<<by.toHex();
#endif
            if(m_serial != NULL){
                m_serial->write(by);
                m_serial->flush();
            }

            timeas+= 10000;

            emit   sigCleanRunTimeout(timeas);
        }


        if(ishot == 102){
            uchar ch[256];
            uchar lens =0;
            uchar lenb =0;
            uchar cha = 0;
            int i = 0;
            ch[0]='W';
            ch[1]='X';
            ch[2]=0x01;
            ch[3]=0x01;
            ch[4]=0x00;
            ch[5]=0x01;
            ch[6]=0x75;
            ch[7]=0x00;
            cha = ch[0];
            for(int k =1 ; k <= 7 ;k++){
                cha += ch[k];
            }
            ch[8] = cha;

            QByteArray by((char * )ch,9);
#ifdef DEBUG_INFOR
            qDebug()<< metaObject()->className()<<__func__<<__LINE__<<by.toHex();
#endif
            if(m_serial != NULL){
                m_serial->write(by);
                m_serial->flush();
            }
            timeas+= 600000;

            timeas+= 10000;

            emit   sigCleanRunTimeout(timeas);
        }

        if(ishot == 1 || ishot == 3 || ishot ==5 || ishot ==17 || ishot ==18 || ishot ==20 || ishot ==21 || ishot ==19){
            int temperature = jsona.take("temperature").toInt();

            uchar ch[256];
            uchar lens =0;
            uchar lenb =0;
            uchar cha = 0;
            int i = 0;
            ch[0]='W';
            ch[1]='X';
            ch[2]=0x01;
            ch[3]=0x01;
            ch[4]=0x00;
            ch[5]=0x01;
            ch[6]=0x72;

            ch[8]=ishot;

            ch[9]=(uchar)(temperature >> 8);
            ch[10]=(uchar)(temperature );


            ch[11]=0x00;
            ch[12]=0x00;
            ch[13]=0x00;
            QJsonArray group = jsona.take("group").toArray();

            for( i = 0; i < group.size() + 1 ;i++){
                if(i <  group.size()){
                    QJsonObject itema = group.at(i).toObject();
                    int drict = itema.take("drict").toInt();

                    int turn = itema.take("turn").toInt();
                    int soak = itema.take("soak").toInt();
                    int speed = itema.take("speed").toInt();
                    timeas+= turn;

                    timeas+= soak;

                    ch[14 + i * 9]=drict;


                    ch[15 + i * 9]=(uchar)(turn >> 16);
                    ch[16 + i * 9]=(uchar)(turn >> 8);//0x13;
                    ch[17+ i* 9]=(uchar)(turn);//0xBB;




                    ch[20 + i * 9]=(uchar)(soak >> 16);
                    ch[21 + i * 9]=(uchar)(soak >> 8);
                    ch[22 + i* 9]=(uchar)(soak);

                    ch[18 + i * 9]=(uchar)(speed );
                    ch[19+ i* 9]=(uchar)(speed );

                }else{
                    ch[14 + i * 9]=0;


                    ch[15 + i * 9]=0x00;
                    ch[16 + i * 9]=0x00;//0x13;
                    ch[17+ i* 9]=0x00;//0xBB;




                    ch[20 + i * 9]=0x00;
                    ch[21 + i * 9]=0x00;
                    ch[22 + i* 9]=0x00;

                    ch[18 + i * 9]=0x10;
                    ch[19+ i* 9]=0x10;
                }



            }


            lenb = 9*4+6 ;

            ch[7]=lenb;
            cha = ch[0];
            for(int k =1 ; k <= (7 + 9*4+6 ) ;k++){
                cha += ch[k];
            }
            ch[8+ 9*4+6] = cha;

            QByteArray by((char * )ch,9  + 9*4+6);
#ifdef DEBUG_INFOR
            qDebug()<< metaObject()->className()<<__func__<<__LINE__<<by.toHex();
#endif
            if(m_serial != NULL){
                m_serial->write(by);
                m_serial->flush();
            }
            if(ishot == 1){
                timeas+= 1500000 * 2;

            }
            timeas+= 10000;

            emit   sigCleanRunTimeout(timeas);
        }
        m_selectCleanIndexs++;
    }else{

        emit sigCleanSuccess(0);

    }


}

void DeviceConversion::handlCloseCmd()
{
#ifndef DEBUG_INFOR
    QString printlogStr=typeid(this).name()+QString(" %1").arg( __func__) +" -- "+ QString("%1").arg( __LINE__)   ;
    PrintLogb::gestance()->printInfor(printlogStr);
#endif
    uchar ch[256];
    uchar lens =0;
    uchar lenb =0;
    uchar cha = 0;
    int i = 0;
    ch[0]='W';
    ch[1]='X';
    ch[2]=0x01;
    ch[3]=0x01;
    ch[4]=0x00;
    ch[5]=0x01;
    ch[6]=0x22;
    ch[7]=0x00;
    cha = ch[0];
    for(int k =1 ; k <= 7 ;k++){
        cha += ch[k];
    }
    ch[8] = cha;

    QByteArray by((char * )ch,9);
#ifdef DEBUG_INFOR
    qDebug()<< metaObject()->className()<<__func__<<__LINE__<<by.toHex();
#endif
    if(m_serial != NULL){
        m_serial->write(by);
        m_serial->flush();
    }
}

int DeviceConversion::getSelectCleanIndexs()
{
    return m_selectCleanIndexs;
}

int DeviceConversion::getRegnLed(QString regions)
{

    return m_regnLed.value(regions);
}

int DeviceConversion::getRegnInforsLed(int regions)
{
    return m_regnInforsLed.value(regions,0);
}

void DeviceConversion::pauseProgram()
{
#ifndef DEBUG_INFOR
    QString printlogStr=typeid(this).name()+QString(" %1").arg( __func__) +" -- "+ QString("%1").arg( __LINE__)   ;
    PrintLogb::gestance()->printInfor(printlogStr);
#endif
    uchar ch[256];
    uchar lens =0;
    uchar lenb =0;
    uchar cha = 0;
    int i = 0;
    ch[0]='W';
    ch[1]='X';
    ch[2]=0x01;
    ch[3]=0x01;
    ch[4]=0x00;
    ch[5]=0x01;
    ch[6]=0x80;
    ch[7]=0x00;
    cha = ch[0];
    for(int k =1 ; k <= 7 ;k++){
        cha += ch[k];
    }
    ch[8] = cha;

    QByteArray by((char * )ch,9);
#ifdef DEBUG_INFOR
    qDebug()<<__func__<<__LINE__<<by.toHex();
#endif
    if(m_serial != NULL){
        m_serial->write(by);
        m_serial->flush();
    }
}

void DeviceConversion::goOnProgram()
{
#ifndef DEBUG_INFOR
    QString printlogStr=typeid(this).name()+QString(" %1").arg( __func__) +" -- "+ QString("%1").arg( __LINE__)   ;
    PrintLogb::gestance()->printInfor(printlogStr);
#endif
    uchar ch[256];
    uchar lens =0;
    uchar lenb =0;
    uchar cha = 0;
    int i = 0;
    ch[0]='W';
    ch[1]='X';
    ch[2]=0x01;
    ch[3]=0x01;
    ch[4]=0x00;
    ch[5]=0x01;
    ch[6]=0x81;
    ch[7]=0x00;
    cha = ch[0];
    for(int k =1 ; k <= 7 ;k++){
        cha += ch[k];
    }
    ch[8] = cha;

    QByteArray by((char * )ch,9);
#ifdef DEBUG_INFOR
    qDebug()<<__func__<<__LINE__<<by.toHex();
#endif
    if(m_serial != NULL){
        m_serial->write(by);
        m_serial->flush();
    }
}

QJsonObject DeviceConversion::currentCleanIndex()
{
    QJsonObject jsona;
    if(m_selectCleanIndexs < m_cleanList.size() ){
        jsona = m_cleanList.at(m_selectCleanIndexs);
    }
    return jsona;
}

int DeviceConversion::currentCleanTime()
{
    quint64 currentTimes = QDateTime::currentDateTime().toTime_t();
    quint64 currentTimea =currentTimes - m_currentTimes;

    return currentTimea;
}

void DeviceConversion::setCleanError(int errors)
{
    m_clearDataError = errors;
}

void DeviceConversion::onDataReceived()
{
#ifdef DEBUG_INFOR
    qDebug()<< metaObject()->className()<<__func__<<__LINE__;
#endif
    if(m_serial != NULL){
#ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__;
#endif
        uint8_t cha[2];
        cha[0]=0x01;
        cha[2]=0x10;
#ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__;
#endif
        QByteArray data = m_serial->readAll();
        m_currentData+=data;
#ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"data=="<<data.toHex();
        qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"m_currentData=="<<m_currentData.toHex();

#endif

        bool isEnd = false;
        int uart3_lens = 0;
        int errors = 0;
        uint8_t g_temp_check = 0x00;
        uint8_t uart3_types = 0x00;
        for (int i = 0; i < m_currentData.size(); i++) {
            if (m_currentData[i] == 'W' && m_currentData[i + 1] == 'X' && (i + 9) <= m_currentData.size()) {
                uart3_lens = (int) m_currentData[i + 7];
                if ((uart3_lens + i + 8) <= m_currentData.size()) {
                    g_temp_check = 0;

                    for (int j = i; j <= (uart3_lens + i + 7); j++) {

                        g_temp_check += m_currentData[j];
                    }
                    uint8_t uart3_type = m_currentData[i + 6];
                    if ( (uint8_t)m_currentData[uart3_lens + i + 8] == g_temp_check) {

                        uart3_types = uart3_type;
                        if (uart3_types == 0x73 || uart3_types == 0x69 || uart3_types == 0x71|| uart3_types == 0x76) {
                            m_clearDataError = (int) m_currentData[i + 8];
                        }
                        isEnd = true;

                        m_currentData.clear();

                    }
                }
            }
        }

        if (isEnd) {
            if (uart3_types == 0x63) {
                //                handlReginLed(m_regions,0);
                //                if(m_outCmdTimer != NULL){
                //                    m_outCmdTimer->start(300);
                //                }
#ifdef DEBUG_INFOR
                qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"m_outTypes=="<<m_outTypes;
#endif
                if(m_outTypes == 1){
                    emit sigSequentialSuccess();

                }
                if(m_outTypes == 2){
                    emit sigSamelSuccess();
                }
            }
            if (uart3_types == 0x73 || uart3_types == 0x69 || uart3_types == 0x71|| uart3_types == 0x76) {
                if(m_cleanTimer != NULL){
                    m_cleanTimer->start(200);
                }
                // onCleanTimeout();
            }
            if (uart3_types == 0x72 || uart3_types == 0x68 || uart3_types == 0x70|| uart3_types == 0x75) {
                //                if(m_cleanTimer != NULL){
                //                    m_cleanTimer->start(200);
                //                }

            }
            if (uart3_types == 0x22) {
                emit  sigCloseCmds();
            }
        }
        //    m_currentData.clear();


    }

}

void DeviceConversion::onOutTimeout()
{
#ifdef DEBUG_INFOR
    qDebug()<< metaObject()->className()<<__func__<<__LINE__;
#endif
    if(m_outTimer != NULL){
#ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__;
#endif
        m_outTimer->stop();
    }
#ifdef DEBUG_INFOR
    qDebug()<< metaObject()->className()<<__func__<<__LINE__;
#endif
    // handlOutCmd();
}

void DeviceConversion::onOutCmdTimeout()
{
#ifdef DEBUG_INFOR
    qDebug()<< metaObject()->className()<<__func__<<__LINE__;
#endif
    if(m_outData.size() > 0){
#ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__;
#endif
        QString regins =  m_outData.keys().at(0);
        m_regions = regins;
        handlReginLed(m_regions,1);
        //   m_outTimer->start(RegionalPins::gestance()->getRegionalTimers(m_regions));
    }else{
#ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__;
#endif
        if(m_outTypes == 1){
            emit sigSequentialSuccess();

        }
        if(m_outTypes == 2){
            emit sigSamelSuccess();
        }
    }
}

void DeviceConversion::onCleanTimeout()
{
#ifndef DEBUG_INFOR
    QString printlogStr=typeid(this).name()+QString(" %1").arg( __func__) +" -- "+ QString("%1").arg( __LINE__)   ;
    PrintLogb::gestance()->printInfor(printlogStr);
#endif
#ifdef DEBUG_INFOR
    qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"m_clearDataError=="<<m_clearDataError;
#endif
    if(m_cleanTimer != NULL){
        m_cleanTimer->stop();
    }
    if(m_clearDataError == 0){

        handlCleanCmd();
    }else{
        emit sigCleanSuccess(m_clearDataError);

    }
}

